package com.cyy.friend.controller;

import com.cyy.friend.service.FriendService;
import com.cyy.friend.service.NoFriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 添加好友 type 0 不喜欢 1喜欢
     */
    @RequestMapping(value = "/like/{friendid}/{type}",method = RequestMethod.GET)
    public Result addFriend(@PathVariable String friendid,@PathVariable String type){
        Claims claims = (Claims) request.getAttribute("user_claims");
        if(claims == null){
            return new Result(false, StatusCode.ACCESSERROR,"无权访问");
        }
        //喜欢
        if("1".equals(type)){
            if(friendService.addFriend(claims.getId(),friendid) == 0 ){
                return new Result(false,StatusCode.REPERROR,"已经添加过此好友");
            }
        }else{
            //不喜欢
            friendService.addNoFriend(claims.getId(),friendid);
        }
        return new Result(true,StatusCode.OK,"操作成功");
    }

    @RequestMapping(value="/{friendid}",method = RequestMethod.DELETE)
    public Result deleteFriend(@PathVariable String friendid){
        Claims claims = (Claims) request.getAttribute("user_claims");
        if(claims == null){
            return new Result(false,StatusCode.ACCESSERROR,"无权访问");
        }
        friendService.deleteFriend(claims.getId(),friendid);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
